var arrayOfEnemies = [];
var arrayOfPlayers = [];

function Bunny(x, y, stage)
{
	var bunny = new PIXI.Sprite(getTexture("bunny"));
	bunny.setInteractive(true);
	bunny.buttonMode = true;
	
	bunny.reloadSpeed = 10;
	bunny.reloadCooldown = 0;
	bunny.sightRange = 128;

	bunny.mousedown = bunny.touchstart = function (data) {
	    //if (bRoundStarted == false) {
	        this.data = data;
	        this.dragging = true;
	    //}
	};
	
	bunny.mouseup = bunny.mouseupoutside = bunny.touchend = bunny.touchendoutside = function (data) {
		gridAlign(bunny);
		this.alpha = 1;
		this.dragging = false;
		this.data = null;
	};
	
	bunny.mousemove = bunny.touchmove = function(data) {
		if(this.dragging)
		{
			var newPosition = this.data.getLocalPosition(this.parent);
			this.position.x = newPosition.x;
			this.position.y = newPosition.y;
		}
	}
	
	bunny.position.x = x;
	bunny.position.y = y;

	gridAlign(bunny);
	stage.addChild(bunny);
	bunny.alive = true;
	bunny.health = 1;
	bunny.kill = function()
	{
		stage.removeChild(bunny);
		bunny.alive = false;
	}
	
	bunny.hitBunny = function (dmgAmt)
	{
		bunny.health -= dmgAmt;
		if (bunny.health <=0)
		{
			bunny.kill();
		} 
	}
	bunny.doLogic = function ()
	{
		if (bunny.reloadCooldown <=0 ) 
		{
			for (var i = 0; i < arrayOfEnemies.length; i++) 
			{	
				var myX = bunny.position.x + 14;
				var myY = bunny.position.y + 14;
				var myW = bunny.position.width = sightRange;
				var myH = bunny.position.height = sightRange;
				if (arrayOfEnimies[i].alive){
					var bX = arrayOfEnimies[j].position.x;
					var bY = arrayOfEnimies[j].position.y;
					var bW = arrayOfEnimies[j].width;
					var bH = arrayOfEnimies[j].height;
					if(DOTHEYHIT(myX,myY,myW,myH,bX,bY,bW,bH) ) 
					{
						var BAM = CreateProjectile(myX, myY, stage);
						//if ()
						BAM.directionX = 0;
						BAM.directionY = -1;
						bunny.reloadCooldown  = bunny.reloadSpeed;
					}
				}
			
			}
		}
		else 
		{
			bunny.reloadCooldown--;
		}
	}
	arrayOfBunnies.push(bunny);
	return bunny;
}

var arrayOfBunnies = [];
function updateEnemies() {
    for (var i = 0; i < arrayOfEnemies.length; i++) {
        arrayOfEnemies[i].doLogic();
    }
}
function updateGoodGuys()
{
  for (var i = 0; i < arrayOfBunnies.length; i++) {
        arrayOfBunnies[i].doLogic();
    }
}

function gridClamp(val) {
    val2 = Math.floor(val / 32);
    val2 = clamp(val2, 2, 13);

    bonus = (val % 32 > 15 ? 1 : 0);
    if (val2 == 2 || val2 == 13) {
        return val2;
    }
    else {
        return val2 + bonus;
    }
}

function clamp(val, min, max) {
    return Math.max(min, Math.min(val, max));
}

function gridAlign(bunny) {
    bunny.position.x = gridClamp(bunny.position.x) * 32;
    bunny.position.y = gridClamp(bunny.position.y) * 32;
}

function gridAlignPos(position) {
    position.x = gridClamp(position.x) * 32;
    position.y = gridClamp(position.y) * 32;
}

/**
* The Enemy object represents an enemy attacking the player's defenses.
* @class Enemy
* @constructor 
* @param position {Point}
* @param sprite {Sprite}
* @param stage {Stage}
*/
function Enemy(position, sprite, stage) {
    this.bDead = false;
    this.position = position;
    this.sprite = sprite;
    this.sprite.position = position;
    stage.addChild(this.sprite);
	this.alive = true;
}

Enemy.prototype.positionAdd = function (x, y) {
    this.position.x += x;
    this.position.y += y;
    gridAlign(this);
    this.sprite.position = this.position;
}

Enemy.prototype.moveSouth = function () {
    this.positionAdd(0, 32);
}

Enemy.prototype.moveNorth = function () {
    this.positionAdd(0, -32);
}

Enemy.prototype.moveWest = function () {
    this.positionAdd(-32, 0);
}

Enemy.prototype.moveEast = function(){
    this.positionAdd(32,0);
}

Enemy.prototype.hitEnemy = function(){
    	this.health -= dmgAmt;
		if (this.health <=0)
		{
			this.kill();
		} 
}
Enemy.prototype.kill = function()
{
	stage.removeChild(this);
	this.alive = false;
}

Enemy.prototype.doLogic = function () { }

function Goat(x,y,stage) {
    var sprite = new PIXI.Sprite(getTexture("goat"));
    var position = new PIXI.Point(x, y);
    var goat = new Enemy(position, sprite, stage);
    goat.explode = function () {
        var balls = CreateProjectile(this.position.x, this.position.y, stage);
        balls = CreateProjectile(this.position.x, this.position.y, stage); balls.directionX = -1; balls.directionY = -1;
        balls = CreateProjectile(this.position.x, this.position.y, stage); balls.directionX = 1; balls.directionY = -1;
        balls = CreateProjectile(this.position.x, this.position.y, stage); balls.directionX = -1; balls.directionY = 1;
        this.bDead = true;
    }
    goat.doLogic = function () {
        if (this.bDead == false) {
            if (this.position.y >= 384 - 32) {
                this.explode();
            }
            else {
                this.moveSouth();
            }
        }
    }
    
    arrayOfEnemies.push(goat);
    return goat;
}

function DennisRodman(x, y) {
    var sprite = new PIXI.Sprite(getTexture("rodman"));
    var position = new PIXI.Point(x, y);
    var rodman = new Enemy(position, sprite, stage);
    rodman.throwGrenade = function () { }
    rodman.doLogic = function () {
        if (this.position.y > 64) {
            this.throwGrenade();
        }
        else {
            this.moveSouth();
        }
    }
    return rodman;
}

function OliveMan(x, y) {
    var sprite = new PIXI.Sprite(getTexture("olive"));
    var position = new PIXI.Point(x, y);
    var olive = new Enemy(position, sprite, stage);
    olive.shoot = function () { }
    olive.doLogic = function () {
        if (this.position.y > 64) {
            this.shoot();
        }
        else {
            this.moveSouth();
            this.shoot();
        }
    }
    return olive;
}

/**
* The Player object represents a unit in the player's defenses.
* @class Player
* @constructor 
* @param position {Point}
* @param sprite {Sprite}
* @param stage {Stage}
*/
function Player(position, sprite, stage) {
    this.position = position;
    this.sprite = sprite;
    stage.addChild(this.sprite);
    sprite.position = position;
    this.alive = true;
}

Player.prototype.doLogic = function () { }

function Tank(x, y, stage) {
    var sprite = new PIXI.Sprite(getTexture(1));
    var position = new PIXI.Point(x, y);
    gridAlignPos(position);
    var tank = new Player(position, sprite, stage);
    tank.shoot = function () {
        var balls = CreateProjectile(this.position.x, this.position.y, stage); balls.directionX = -1; balls.directionY = -1;
        balls = CreateProjectile(this.position.x, this.position.y, stage); balls.directionX = 1; balls.directionY = -1;
    }
    tank.doLogic = function () {
        this.shoot();
        }

        arrayOfPlayers.push(tank);
    return tank;
}

function TemplateBox(position, sprite, stage) {
    this.position = position;
    this.sprite = sprite;
    stage.addChild(this.sprite);
    sprite.position = position;
}

function TankTemplate(x, y, stage, i) {
    var sprite = new PIXI.Sprite(getTexture(i+1));
    var position = new PIXI.Point(x, y);

    var TankTemplate = new TemplateBox(position, sprite, stage);
    sprite.setInteractive(true);
    sprite.buttonMode = true;
    sprite.scale.x = 2.5;
    sprite.scale.y = 2.5;

    sprite.click = sprite.tap = function (data) {
        var tank = new Tank(64 + Math.random() * 352, 384+32- Math.random()*64, stage);
        arrayOfPlayers.push(tank);
    };
}

function CreateTemplate(x, y, stage, i) {
    TankTemplate(x, y, stage, i);
}

function StartButton(x, y, stage) {
    var sprite = new PIXI.Sprite(getTexture("tank"));
    var position = new PIXI.Point(x, y);

    sprite.click = sprite.tap = function (data) {
        startRound();
    };
}

function startRound() {
    bRoundStarted = true;
}

function stopRound() {
    bRoundStarted = false;
}

function runGUIMethods() {
    return 0;
}